using System;
using System.Text;
using System.Reflection;
using System.Data.SqlClient;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Crainiate.Data.Persistence;
using Crainiate.Data.Persistence.Cache;
using Crainiate.Data.Persistence.Providers.SqlServer;

namespace Crainiate.Data.Persistence.Testing
{
    [TestClass]
    public class InheritanceTests
    {
        //Make sure that an object is only selected when all keys are in the inheritance chain
        [TestMethod, ExpectedException(typeof(RecordNotFoundException))]
        public void SelectPartial()
        {
            Person person = new Person(Component.Instance.GetContext().CreateCommand());
            person.Id = 101;
            person.FirstNames = "Joe";
            person.Insert();

            Customer customer = new Customer(Component.Instance.GetContext().CreateCommand());
            customer.Id = 101;
            customer.Select(); //Should throw exception
        }

        //Test that an object can be inserted when certain keys are available down the chain
        [TestMethod, ExpectedException(typeof(DuplicateKeyException))]
        public void InsertCustomerPartial()
        {
            Person person = new Person(Component.Instance.GetContext().CreateCommand());
            person.Id = 102;
            person.FirstNames = "Mark";
            person.Insert();

            Customer customer = new Customer(Component.Instance.GetContext().CreateCommand());
            customer.Id = 102;
            customer.FirstNames = "Bob";
            customer.Insert();
        }

        //Test the promotion of an object does not cause a duplicate key error
        [TestMethod]
        public void PromoteCustomer()
        {
            Person person = new Person(Component.Instance.GetContext().CreateCommand());
            person.Id = 103;
            person.FirstNames = "Paul";
            person.Insert();

            Customer customer = new Customer(Component.Instance.GetContext().CreateCommand());
            customer.Id = 103;
            customer.FirstNames = "Bob";

            customer.WithInheritance = false;
            customer.Insert();

        }

        [TestMethod, ExpectedException(typeof(RecordNotFoundException))]
        public void DemoteCustomer()
        {
            Person person = new Person(Component.Instance.GetContext().CreateCommand());
            person.Id = 104;
            person.FirstNames = "Paul";
            person.Insert();

            Customer customer = new Customer(Component.Instance.GetContext().CreateCommand());
            customer.Id = 104;
            customer.FirstNames = "Bob";

            customer.WithInheritance = false;
            customer.Insert();

            customer = new Customer(Component.Instance.GetContext().CreateCommand());
            customer.Id = 104;
            customer.WithInheritance = false;
            customer.Delete();

            try
            {
                person = new Person(Component.Instance.GetContext().CreateCommand());
                person.Id = 104;
                person.Select();
            }
            catch (Exception ex)
            {
                throw new Exception("Person should be selected after demotion.", ex);
            }

            customer = new Customer(Component.Instance.GetContext().CreateCommand());
            customer.Id = 104;
            customer.Select();

        }


    }
}
